<template>
  <view>
    <!--pages/connectDetail/deviceDetail.wxml-->
    <view class="bluetooth-detail">
      <view>当前连接的蓝牙设备是：</view>
      <view class="device-list">
        <view>设备名：{{ info.deviceName }}</view>
        <view>设备ID：{{ info.macId }}</view
        >˚
      </view>
    </view>
    <view class="card">
      <view>日志(可滑动查看)：</view>
      <button class="btn" @tap="clearLogs">清空日志</button>
      <view>
        <scroll-view scroll-y="true" class="text-box list">
          <view user-select="true" >{{ textLog }}</view>
        </scroll-view>
      </view>
    </view>
    <view class="opt-group">
      <view class="opt-btn-block">
        <button class="btn" @tap="getHistory">获取历史记录</button>
      </view>
    </view>
  </view>
</template>

<script>
var app = getApp();
// import sdkAction from "mcbluetoothsdk";

export default {
  data() {
    return {
      textLog: "",
      info: {},
    };
  },

  components: {},
  props: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.info = app.globalData.bindDeviceList[0]
    this._getBlueAdapterState();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log(889);
    if (uni.setKeepScreenOn) {
      uni.setKeepScreenOn({
        keepScreenOn: true,
        success: function (res) {
          console.log("正保持屏幕常亮");
        },
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},
  methods: {
    clearLogs() {
      this.textLog = ''
    },
    _getBlueAdapterState() {
      let _this = this
      uni.getBluetoothAdapterState({
        success(data) {
          if (data.available) {
            _this._connect();
          }
        },
        fail(err) {
          console.log('getBluetoothAdapterState', err)
        }
      });
    },
    _connect() {
      let bindDeviceList = app.globalData.bindDeviceList;
      console.log(bindDeviceList);
      bindDeviceList.map((v) => {
        this.connectOnce(v.macId, v.deviceType);
      });
    },
    connectOnce(macId, deviceType) {
      let params = app.globalData.authParams;
      params.deviceMac = this.info.macAddr
      this.$snJW.createBLEConnection(macId, (res) => {
        console.log(1,res);
        this._onBLECharacteristicValueChange();
      }, uni, 20000, params);
    },
    _onBLECharacteristicValueChange() {
      uni.onBLECharacteristicValueChange((res) => {
        let obj = this.$snJW.parseData(res)
        if (obj && typeof obj === 'object') {
          obj = JSON.stringify(obj)
          this.textLog += obj + '\n'
        }
      });
    },
    getHistory: function () {
      let bindDeviceList = app.globalData.bindDeviceList;
      this.$snJW.sendGetHistCode(
        bindDeviceList[0].macId,
        (res) => {
          console.log(res);
        },
        uni
      );
    },
  },
};
</script>
<style>
/* pages/connectDetail/deviceDetail.wxss */
.bluetooth-detail {
  width: 95%;
  margin: 0 auto;
  padding: 10px 20px;
  box-sizing: border-box;
  border-bottom: 1px solid #ddd;
}

.card {
  border-radius: 10rpx;
  background-color: #fff;
  margin: 20rpx;
  padding: 20rpx;
  min-height: 500rpx;
  box-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.3);
}

.list {
  max-height: 45vh;
}

.opt-input {
  width: 80%;
  position: relative;
  font-size: 16rpx;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  margin: 0 10%;
}

.input {
  border: 2rpx solid #ccc;
  width: 400rpx;
  padding: 16rpx 20rpx;
  font-size: 32rpx;
}

.opt-group {
  width: 100%;
  position: absolute;
  bottom: 6%;
  font-size: 16rpx;
}

.opt-btn-block {
  margin-top: 60rpx;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
}

.opt-btn-block .btn {
  width: 300rpx;
  float: left;
}
</style>
